package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrtegyStatisVOService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

//从sql中查询数据同步到redis数据库中(redis数据预热)
@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrtegyStatisVOService strtegyStatisVOService;
    //spring容器启动时,并且初始化完成之后,马上操作
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        List<StrategyDetail> list = strategyDetailService.list();
        for (StrategyDetail detail : list) {
            //如果vo数据已经存在了,不需要初始化,防止数据被覆盖
            if(strtegyStatisVOService.isStrategyStatisVO(detail.getId())){
                continue;//跳过
            }
            StrategyStatisVO vo = new StrategyStatisVO();
            BeanUtils.copyProperties(detail,vo);//同属性名可以拷贝,其他的不行
            vo.setDestId(detail.getDest().getId());
            vo.setDestName(detail.getDest().getName());
            vo.setStrategyId(detail.getId());
            strtegyStatisVOService.setStrategyStatisVO(vo);
        }
        System.out.println("--------------------------------------------------------------------------------------------");
        System.out.println("--------ZSET初始化------------------------------------------------------------");

        //热门攻略排行初始化
        for (StrategyDetail detail : list) {
            //如果zset中存在某个属性,则需要跳过,否则会一直增加
            String voId = RedisKeys.STRATGY_STATIS_VO.join(detail.getId().toString());
            String zsetKey = RedisKeys.STRATEGY_HOT_ZSET.getPrefix();
            //判断某个void是否存在指定的zset中
            if(strtegyStatisVOService.isVoidExsitZset(voId,zsetKey)){
                continue;
            }
            int score = detail.getViewnum()+detail.getReplynum();
            strtegyStatisVOService.addScoreHot(detail.getId(),score);
        }


        //国内外攻略排行初始化
        for (StrategyDetail detail : list) {
            //如果zset中存在某个属性,则需要跳过,否则会一直增加
            String voId = RedisKeys.STRATGY_STATIS_VO.join(detail.getId().toString());
            String zsetKey = null;
            if(detail.isIsabroad()){
                //国外
                zsetKey = RedisKeys.STRATEGY_ABROAD_ZSET.getPrefix();
            }else {
                //国内
                zsetKey = RedisKeys.STRATEGY_CHINA_ZSET.getPrefix();
            }
            //判断某个void是否存在指定的zset中
            if(strtegyStatisVOService.isVoidExsitZset(voId,zsetKey)){
                continue;
            }
            int score = detail.getFavornum()+detail.getThumbsupnum();
            strtegyStatisVOService.addScoreAbroadAndChina(detail.getId(),score);
        }

    }
}
